// const BASE_URL = "http://127.0.0.1:3000/api";
const BASE_URL = "https://server.boyuanhulian.com";
const getToken = () => {
  return null;
};
async function http({
  method = "GET",
  url,
  data = null,
  responseType = "json",
  headers,
  noToken = false,
  cache = "default",
  signal,
}) {
  const AUTH_TOKEN = noToken ? null : getToken();
  const config = {
    method,
    headers: new Headers({
      "Content-Type": "application/json; charset=utf-8",
      ...headers,
      ...(AUTH_TOKEN ? { Authorization: `Bearer ${AUTH_TOKEN}` } : {}),
    }),
    ...(data ? { body: data } : {}),
    cache,
    signal,
  };
  if (["POST", "PUT", "PATCH"].includes(method) && typeof data === "object") {
    config.body = JSON.stringify(data);
  }
  try {
    const response = await fetch(BASE_URL + url, config);
    // console.log(response, await response.json());
    if (!response.ok) {
      throw new Error(`HTTP error! status: ${response.status}`);
    }
    switch (responseType) {
      case "json":
        return response.json();
      case "text":
        return response.text();
      case "blob":
        return response.blob();
      case "arrayBuffer":
        return response.arrayBuffer();
      case "formData":
        return response.formData();
      default:
        throw new Error("Unsupported response type");
    }
  } catch (error) {
    // console.error("There was a problem with the fetch operation:", error);
    throw error;
  }
}
